package com.cxrj.zhsq.workorderManage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.workorderManage.dao.PropertyTypeMapper;
import com.cxrj.zhsq.workorderManage.entity.PropertyType;
import com.cxrj.zhsq.workorderManage.service.PropertyTypeService;

@Service
public class PropertyTypeServiceImpl implements PropertyTypeService {

	@Resource
	private PropertyTypeMapper propertyTypeMapper;

	@Override
	public Map<String, Object> seletePropertyTypePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = propertyTypeMapper.selectByCount(sqlMap) + "";
		List<PropertyType> list = propertyTypeMapper.selectPropertyTypesPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> insertPropertyType(PropertyType propertyType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (propertyType.getId().equals("")) {
			propertyType.setId(UUID.randomUUID().toString());
			propertyType.setStatus("1");
			propertyType.setCreateTime(new Date());
			c = propertyTypeMapper.insertSelective(propertyType);
		} else {
			c = propertyTypeMapper.updateByPrimaryKeySelective(propertyType);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updatePropertyType(PropertyType propertyType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = propertyTypeMapper.updateByPrimaryKeySelective(propertyType);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectPropertyTypeById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("type", propertyTypeMapper.selectByPrimaryKey(id));
		return resultMap;
	}

	@Override
	public Map<String, Object> selectPropertyTypeByAll(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		List<PropertyType> list = propertyTypeMapper.selectByAll(sqlMap);
		resultMap.put("list", list);
		return resultMap;
	}

}
